package com.demo.mystudy.ui.socketclient

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.demo.mystudy.R
import com.demo.mystudy.databinding.DialogSocketclientBinding
import com.demo.mystudy.socketbean.SimpleSocketBean
import com.demo.mystudy.ui.socketserver.SocketAdapter
import com.jetinno.simple.SimpleDialogFragment
import com.jetinno.simple.utils.FragmentsUtil
import com.jetinno.simple.utils.JsonUtil
import kotlinx.coroutines.launch
import timber.log.Timber

/**
 * Created by zhangyuncai on 2025/4/27.
 * 描述：
 */
class SocketClientDF : SimpleDialogFragment() {
    lateinit var mDB: DialogSocketclientBinding
    private val mAdapter by lazy { SocketAdapter(WebSocketClientManager.mDatas) }
    override fun useDatabing(inflater: LayoutInflater, container: ViewGroup?): View? {
        mDB = DataBindingUtil.inflate(inflater, R.layout.dialog_socketclient, container, false)
        return mDB.root
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_socketclient
    }

    override fun initView() {
        mDB.etSocketclientIp.setText("10.0.2.16")
        mDB.rvSocketclientReceivemsg.run {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }
        mDB.cbSocketclientIsconnected.isChecked = WebSocketClientManager.isConnected
    }

    var index = 0
    override fun initEvent() {
        WebSocketClientManager.callback = object : SocketClientCallback {
            override fun addData(msg: String) {
                lifecycleScope.launch {
                    mAdapter.notifyDataSetChanged()
                }
            }

            override fun onConnectStausChanged(isConnected: Boolean) {
                lifecycleScope.launch {
                    mDB.cbSocketclientIsconnected.isChecked = WebSocketClientManager.isConnected
                }
            }

        }
        mDB.btSocketclientConnect.setOnClickListener {
            val ip = mDB.etSocketclientIp.text.toString().trim()
            Timber.i("链接服务器,ip:${ip}")
            WebSocketClientManager.connect(ip, 9527)
        }
        mDB.btSocketclientDisconnect.setOnClickListener {
            WebSocketClientManager.close()
        }
        mDB.btSocketclientSend.setOnClickListener {
            val simpleSocketBean = SimpleSocketBean()
            simpleSocketBean.content = "你好啊:${index++}"
            simpleSocketBean.cmd = "test"
            WebSocketClientManager.sendMessage(JsonUtil.anyToJson(simpleSocketBean))
        }
        mDB.headbar.setBackClickListener {
            finish()
        }
    }

    override fun initData() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        WebSocketClientManager.callback = null
    }

    companion object {
        fun showSocketClientDF(fragmentManager: FragmentManager, context: Context) {
            FragmentsUtil.showDialog(fragmentManager, context, SocketClientDF::class.java)
        }
    }

}